//
//  DetailViewController.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
#import "NovelDataBase.h"
#import "CollectModel.h"
#import "NovelDataBase.h"
#import "DetaiCollectionViewCell.h"
#import "CollectButton.h"
@interface DetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic,retain)UIView *headerView;
@property (nonatomic,retain)UIActivityIndicatorView *acticity;
@property (nonatomic,retain)UILabel *titleLable;
@property (nonatomic,retain)UILabel *detailLable;
@property (nonatomic,retain)UILabel *countLable;
@property (nonatomic,copy)NSString *countStr;
@property (nonatomic,assign)BOOL judge;
@property (nonatomic,assign)NSInteger item;
@property (nonatomic,retain)CollectButton *collecButton;
@end

@implementation DetailViewController
-(void)dealloc
{
    
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.arr = [NSMutableArray array];
        
        self.collecButton = [[CollectButton alloc]initWithFrame:CGRectMake(0 * OffWidth, 155 * OffHeight, 90* OffWidth, 25 * OffHeight)];
        
        [_collecButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
       

    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCollectionView];
    [self addDownDetail];
    self.view.backgroundColor = [UIColor blackColor];
    [self addHeaderView];

}
- (void)addHeaderView
{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60 * OffHeight)];
    _headerView.backgroundColor = [UIColor blackColor];
    _headerView.alpha = 0.5;
    [self.view addSubview:_headerView];
    [_headerView release];
   

    
    
    self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(40 * OffWidth,20 * OffHeight, 300 * OffWidth ,30 * OffHeight)];
        self.titleLable.textColor = [UIColor whiteColor];
        self.titleLable.font = [UIFont boldSystemFontOfSize:15];
        self.titleLable.textAlignment = 1;
    if (self.temp == 0) {
        
   
    self.titleLable.text  = [[[[[self.arr objectAtIndex:self.index] objectForKey:@"annotations"] objectAtIndex:0]objectForKey:@"place"] objectForKey:@"title"];

           }else{
               self.titleLable.text  =[[self.arr objectAtIndex:self.index] objectForKey:@"title"];
    }
    [self.view addSubview:self.titleLable];
    [self.titleLable release];
    


}




- (void)addCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width, 500);
    [flow setMinimumLineSpacing:0];
    
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    UICollectionView *collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 450 * OffHeight) collectionViewLayout:flow];
    collect.dataSource = self;
    collect.delegate = self;
    collect.pagingEnabled = YES;

    collect.contentOffset = CGPointMake(self.index * self.view.frame.size.width, 0);
    [self.view addSubview:collect];
    
    
    [collect registerClass:[DetaiCollectionViewCell class] forCellWithReuseIdentifier:@"ff"];
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.temp == 0) {
        
  
    self. judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:indexPath.item]objectForKey:@"original_pic" ]];
    }else{
         self. judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:indexPath.item]objectForKey:@"image" ]];
    }

    DetaiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ff" forIndexPath:indexPath];

    self.item = indexPath.item;

    
    if (self.temp == 0) {
        
   
    [cell.imageV sd_setImageWithURL:[[self.arr objectAtIndex:indexPath.item]objectForKey:@"original_pic" ]];
        
     }else{
         
         [cell.imageV sd_setImageWithURL:[[self.arr objectAtIndex:indexPath.item]objectForKey:@"image"]];
         
     }
    return cell;
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.arr.count;
    
    
}

- (void)addDownDetail
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 200 * OffHeight, self.view.frame.size.width, 200 * OffHeight)];
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self.view addSubview:view];
    
    
    self.detailLable = [[ UILabel alloc]initWithFrame:CGRectMake(10 * OffWidth, 10 * OffHeight, self.view.frame.size.width, 100 * OffHeight)];
    _detailLable.textColor = [UIColor whiteColor];
    _detailLable.numberOfLines = 0;
    _detailLable.font = [UIFont boldSystemFontOfSize:15];
    _detailLable.text = [[[self.arr objectAtIndex:self.index] objectForKey:@"user"] objectForKey:@"description"];
    [view addSubview:_detailLable];
    
    self.countStr = [NSString stringWithFormat:@"%ld/%ld",self.index + 1,self.arr.count];
    self.countLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * OffWidth, 30 * OffHeight)];
    self.countLable.text = _countStr;
    self.countLable.textColor = [UIColor whiteColor];
    [view addSubview:_countLable];
    

    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonBack.frame = CGRectMake(self.view.frame.size.width / 2 - 15 * OffWidth,  150 * OffHeight, 30 * OffWidth, 30 * OffHeight);
    [buttonBack setBackgroundImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
    
    [buttonBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buttonBack];
    

     [view addSubview:_collecButton];
    
    
    if (self.temp == 0) {
        
   
      BOOL judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:self.index]objectForKey:@"original_pic"]];

    if (judge == YES) {
        
        self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
        self.collecButton.collectLable.text = @"已收藏";
        
        
    }else{
        
        self.collecButton.collectLable.text = @"收藏";
        self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
    }
    }else{
        
        BOOL judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:self.index]objectForKey:@"image"]];
        
        if (judge == YES) {
            
            self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
            self.collecButton.collectLable.text = @"已收藏";
            
            
            
        }else{
            
            self.collecButton.collectLable.text = @"收藏";
            self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];

        }
    }
   
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
        self.countStr = [NSString stringWithFormat:@"%ld/%ld",self.item + 1,self.arr.count];
        self.countLable.text = _countStr;
        

    if (self.temp == 0) {
                self.titleLable.text  = [[[[[self.arr objectAtIndex:self.item] objectForKey:@"annotations"] objectAtIndex:0]objectForKey:@"place"] objectForKey:@"title"];
        _detailLable.text = [[[self.arr objectAtIndex:self.item] objectForKey:@"user"] objectForKey:@"description"];
    }else {
        self.titleLable.text = [[self.arr objectAtIndex:self.item] objectForKey:@"title"];
//        _detailLable.text = [[self.arr objectAtIndex:self.item] objectForKey:@"title"];
    }
    
    
    if (self.judge == YES) {
        
        self.collecButton.collectLable.text = @"收藏";
        self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
        
    }else{
        
        self.collecButton.collectLable.text = @"已收藏";
        self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];

    }
    
    
    
   
}

- (void)collect:(UIButton *)button
{
   
    if (self.temp == 0) {
        BOOL judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:self.item]objectForKey:@"original_pic" ]];
        
        if (judge) {
            
            CollectModel *model = [[CollectModel alloc]init];
            model.imageStr = [[self.arr objectAtIndex:self.item] objectForKey:@"original_pic"] ;
            [NovelDataBase deleteCollectWithModel:model];
            self.collecButton.collectLable.text = @"收藏";
            self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
            [model release];
            
        }else{
            
            NSString *image = [[self.arr objectAtIndex:self.item]objectForKey:@"original_pic"];
            NSString *t = [[[[[self.arr objectAtIndex:self.item] objectForKey:@"annotations"] objectAtIndex:0] objectForKey:@"place"] objectForKey:@"title"];
            CollectModel *model = [[CollectModel alloc]initWithCollectImageStr:image title:t];
            [NovelDataBase insertCollectWithModel:model];
            [model release];
            
            self.collecButton.collectLable.text = @"已收藏";
            self.collecButton.collectImage.image = [UIImage imageNamed:@"yishoucang.png"];
            
        }

    }else{
        BOOL judge = [NovelDataBase selectCollectWith:[[self.arr objectAtIndex:self.item]objectForKey:@"image" ]];
        
        if (judge) {
            
            CollectModel *model = [[CollectModel alloc]init];
            model.imageStr = [[self.arr objectAtIndex:self.item] objectForKey:@"image"] ;
            [NovelDataBase deleteCollectWithModel:model];
            self.collecButton.collectLable.text = @"收藏";
            self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];
            [model release];
            
        }else{
            
            NSString *image = [[self.arr objectAtIndex:self.item]objectForKey:@"image"];
            NSString *t = [[self.arr objectAtIndex:self.item] objectForKey:@"title"] ;
            CollectModel *model = [[CollectModel alloc]initWithCollectImageStr:image title:t];
            [NovelDataBase insertCollectWithModel:model];
            [model release];
            
            self.collecButton.collectLable.text = @"已收藏";
            self.collecButton.collectImage.image = [UIImage imageNamed:@"shoucnag.png"];

            
            
        }
        

    }
   
  
}
- (void)backClick:(UIButton *)button

{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
